<template>
	<view class="body">
		<!-- 订单列表 -->
		<u-empty v-if="orderList.length==0" mode="order" icon="http://cdn.uviewui.com/uview/empty/order.png">
		</u-empty>
		<view v-for="item in orderList" :key="item.orderId" class="order">
			<view style="display: flex;">
				<view class="info">
					<view>创建时间: {{item.createTime}}</view>
					<view>认养方案{{item.type + 1}}</view>
					<view>鸽笼个数: {{item.cageNumber}} </view>
					<view>认养时长: {{item.adoptionTime}}</view>
					<view>订单总价: ￥{{item.totalPrice}}</view>
					<view>留言:{{item.message}}</view>
				</view>
				<view v-if="orderStatus==0">
					<view class="paymentButton">
						<u-button type="success" @click="pay(item)">支付</u-button>
					</view>
					<view class="operation">
						<image class="editBtn" src="https://gulimall-void.oss-cn-guangzhou.aliyuncs.com/icon/edit2.png"
							@click="editHandle(item.orderId,item.message)"></image>
						<image class="delBtn" src="https://gulimall-void.oss-cn-guangzhou.aliyuncs.com/icon/delete.png"
							@click="deleteHandle(item.orderId)">
						</image>
					</view>
				</view>
			</view>
		</view>
		<u-popup :show="editShow" mode="center" round="5%" @close="editShow=false">
			<view class="message">
				<u--textarea v-model="message" height="140" count placeholder="请输入您的需求">
				</u--textarea>
			</view>
			<view>
				<u-button type="success" @click="submit">提交</u-button>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import webSocket from '@/util/websocket.js'
	export default {
		name: 'AdoptionOrder',
		props: ['orderStatus'],
		data() {
			return {
				//订单状态枚举
				adoptionOrderStatus: {
					NOT_PAY: 0,
					HANDLING: 1,
					FINISH: 2,
					CANCEL: 3
				},
				status: 0,
				orderList: [],
				message: '',
				orderId: '',
				editShow: false,
				//自动分配鸽笼
				autoAssing: false
			}
		},
		beforeMount() {
			
			this.getOrderList()
			if (this.status == 0) {
				//获取自动分配鸽笼参数
				this.getAutoAssignValue()
			}
		},
		methods: {
			//获取自动分配鸽笼参数
			async getAutoAssignValue() {
				const res = await this.$myRequest({
					url: 'shop/params/get/AUTO_ASSIGN'
				})
				if (res && res.code === 0) {
					this.autoAssign = res.data ? true : false;
				}
			},
			async getOrderList() {
				const res = await this.$myRequest({
					url: 'order/applet/getAllAdoptionOrder/' + this.status
				})
				if (res && res.code === 0) {
					this.orderList = res.data
				}
			},
			changeOrderStatus(index) {
				this.status = index
				this.getOrderList()
			},
			async pay(order) {
				if (this.autoAssign) {
					//自动分配鸽笼，显示提示消息
					uni.showLoading({
						title: '分配鸽笼...',
						icon: 'none'
					})
				}
				const res = await this.$myRequest({
					url: 'order/applet/adoptionOrderPay/' + order.orderId,
					method: 'PUT'
				})
				if (res && res.code === 0) {
					uni.showToast({
						title: res.msg,
						icon: 'none',
					})
					this.getOrderList();
					this.updatePoints(order.cageNumber);
					webSocket.sendSocketMessage("有新的认养订单了", 1);
					wx.requestSubscribeMessage({
					  tmplIds: [''],
					  success (res) { 
						  console.log(res)
					  }
					})
				}
			},
			async updatePoints(count) {
				const res = await this.$myRequest({
					url: 'user/pointsRecord/save',
					method: 'post',
					data: {
						activityId: 1,
						count
					}
				})
				if (res && res.code === 0) {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					})
				}
			},
			editHandle(orderId, message) {
				console.log(orderId)
				this.orderId = orderId;
				this.message = message;
				this.editShow = true;
			},
			deleteHandle(id) {
				uni.showModal({
					title: '确定要取消订单吗?',
					success: async res => {
						if (res.confirm) {
							uni.showLoading()
							const res = await this.$myRequest({
								url: 'order/adoptionorder/delete',
								method: 'delete',
								data: [id]
							})
							uni.hideLoading()
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							this.getOrderList();
						}
					}
				})
			},
			//修改留言
			submit() {
				uni.showModal({
					title: '确定要提交吗?',
					success: async res => {
						if (res.confirm) {
							uni.showLoading()
							const res = await this.$myRequest({
								url: 'order/adoptionorder/update',
								method: 'PUT',
								data: {
									orderId: this.orderId,
									message: this.message
								}
							})
							uni.hideLoading()
							uni.showToast({
								title: res.msg,
								icon: 'none'
							})
							this.editShow = false;
							this.getOrderList();
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import '@/static/iconfont/iconfont.css';

	.order {
		width: 700rpx;
		background-color: #FFFFFF;
		margin: 10px;
		border-radius: 10px;

		.info {
			padding: 2%;
			color: #8a8a8a;
			font-size: 35rpx;
			width: 75%;
		}
	}

	.message {
		width: 450rpx;
	}

	.paymentButton {
		margin-top: 35%;
	}

	.operation {
		margin-top: 20%;
		display: flex;

		image {
			width: 70rpx;
			height: 70rpx;
		}
	}
</style>
